package com.sangmo.fts.basic.service;

import com.sangmo.boot.framework.cache.meta.Cache;
import com.sangmo.boot.framework.plugin.dataview.crud.service.DelegateViewService;
import com.sangmo.boot.framework.plugin.dataview.crud.service.ViewService;
import com.sangmo.fts.basic.model.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class GoodsService extends DelegateViewService<Goods> {

    @Autowired
    private GoodsEntityService goodsEntityService;

    @Override
    protected ViewService<Goods> getEntityService() {
        return goodsEntityService;
    }

    @Cache(value = 300)
    private Collection<Goods> getAllGoodsCache() {
        return goodsEntityService.findAll();
    }

    public Map<Long, Goods> getAllGoodsMapCache() {
        Map<Long, Goods> ret = new HashMap<>();
        Collection<Goods> list = getAllGoodsCache();
        for (Goods b: list) {
            ret.put(b.getId(), b);
        }
        return ret;
    }
}
